home *** CD-ROM | disk | FTP | other *** search
- Path: news.infi.net!usenet
- From: nngis@norfolk.infi.net (Greg DiGiorgio)
- Newsgroups: comp.lang.c,comp.lang.objective-c
- Subject: Re: Comma Delimited function wanted
- Date: 11 Jan 1996 16:13:51 GMT
- Organization: Customer of InfiNet
- Distribution: world
- Message-ID: <4d3cvv$fsm@news.infi.net>
- References: <4d1l42$mb6@voyager.Internex.NET>
- Reply-To: nngis@norfolk.infi.net
- NNTP-Posting-Host: h-langoliers.norfolk.infi.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <4d1l42$mb6@voyager.Internex.NET>, ian_stewart@nyro.com
- says...
- >
- >Help with a comma delimited function needed.
- >
- >I did this once before, but I am blocked and can't
- >find the original source.
- >
- >Here is what I need to do:
- >
- >I have a buffer full of text.
- >
- >char buffer = "3740067099,914885AC2,P03,5000";
- >
- >I have four char vars to put this into.
- >
- >char field1[20], field2[20], field3[20], field4[20];
- >
- >What I want to do is get this as the end result.
- >
- >
- >field1 = 3740067099
- >field2 = 914885AC2
- >field3 = P03
- >field4 = 5000
- >
- >
- >Anyway, help is appreciated.
- >
- >ian
-
- Ian, here's your answer. The key is the "strtok" function.
-
- #include <string.h>
- #include <stdio.h>
- void main (void) {
- char buf[]={"12345,67890,12345"};
- char *s;
-
- s=strtok(buf,","); /* First call to strtok, pass entire buffer */
- while (s) { /* While strtok returns a non-NULL value, loop */
- printf("%s\n",s); /* display parsed value */
- s=strtok(NULL,","); /* Get next value - notice the NULL */
- }
-
- }
-
-
-
-